Add clampedNumberValidator for input fields#157
Conversation
Part of the solution for code-dot-org/dance-party#321 Allows us to have a freeform text input in a block that can be constrained to accepting numeric input within a specified range. The range can have a lower bound, and upper bound, or both. The corresponding input element will be given the "number" type by default so the student cannot even type non-numeric characters into the field. Invalid or blank input results in zero, unless zero is outside the given bounds in which case the zero is clamped to the nearest boundary.
Codecov Report
@@ Coverage Diff @@
## master #157 +/- ##
==========================================
- Coverage 55.55% 55.48% -0.07%
==========================================
Files 96 96
Lines 11476 11486 +10
==========================================
- Hits 6375 6373 -2
- Misses 5101 5113 +12
Continue to review full report at Codecov.
|
Depends on code-dot-org/blockly#157 A custom block can give any field input a type of `"ClampedNumber(min,max)"` where `min` and `max` are optional and define bounds for acceptable number input to the field.
|
See it in action on code-dot-org/code-dot-org#26124 |
Hamms
left a comment
There was a problem hiding this comment.
Sorry for missing this yesterday! One comment, otherwise LGTM!
Also: those tests 😍
| if (this.changeHandler_ === Blockly.FieldTextInput.numberValidator) { | ||
| if ( | ||
| (this.changeHandler_ === Blockly.FieldTextInput.numberValidator) || | ||
| (this.changeHandler_ && this.changeHandler_.validatorType === 'clampedNumberValidator')) |
There was a problem hiding this comment.
I don't love that we're introducing inconsistency with the way you identify a changeHandler; can we create an issue to track improving this?
There was a problem hiding this comment.
👍 That's a good idea. I messed around with identifying these by function name or prototype instead of by identity, but decided in the end it'd be too clever by half and I should just add a property. I'd love to revisit this and make it make sense.
Picks up code-dot-org/blockly#157 which is essential for this change, as well as code-dot-org/blockly#156 which we wanted to ship anyway, and code-dot-org/blockly#152 from a contributor.
Depends on code-dot-org/blockly#157 A custom block can give any field input a type of `"ClampedNumber(min,max)"` where `min` and `max` are optional and define bounds for acceptable number input to the field.
Part of the solution for code-dot-org/dance-party#321
Allows us to have a freeform text input in a block that can be constrained to accepting numeric input within a specified range. The range can have a lower bound, and upper bound, or both. The corresponding input element will be given the "number" type by default so the student cannot even type non-numeric characters into the field. Invalid or blank input results in zero, unless zero is outside the given bounds in which case the zero is clamped to the nearest boundary.